<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  List Offers (/admin/list_offers.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require dirname(__FILE__) . "/../config.php";


// Start session if not already started
session_start();

if(!isset($_SESSION['login_t'])) {

	/* If we're not logged in, take us to the login page and
	ensure the rest of the code isn't run */

	header("Location: /admin/login.php");
	exit;
}

$pagetitle = "List Offers (Admin)";
$pageheading = "<a href=\"/admin/index.php\">" . SITE_NAME . " Administration</a> &gt; List Offers";
$page_content = "<hr />\n";

if(!isset($_GET['cid']) || !is_numeric($_GET['cid'])) {
	// No campaign to list offers for
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No campaign ID was selected for a list of associated offers. To get a list of offers for a specific campaign, click its name on the List Campaigns page.</div>\n";
} else if ($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $db->escape($_GET['cid']) . "'") == false) {
	// Non-existent campaign
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The campaign selected does not exist. Please select another campaign by clicking its name on the List Campaigns page.</div>\n";
} else if ($db->get_results("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `campaign_id` = '" . $_GET['cid'] . "'") == false) {
	$campaign = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $db->escape($_GET['cid']) . "'");
	$page_content .= "<h2>Campaign '" . $campaign->campaign_name . "'</h2>\n";
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No active offers.</div>\n";
} else {
	$campaign = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $db->escape($_GET['cid']) . "'");
	$offers = $db->get_results("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `campaign_id` = '" . $db->escape($_GET['cid']) . "'");
	// Output table of offers
	$page_content .= "<h2>Campaign '" . $campaign->campaign_name . "'</h2>\n";
	$page_content .= "<table class=\"table-80\">\n";
	$page_content .= "<tr><td>Offer #</td><td>Offer Line 1</td><td>Offer Line 2</td><td>Offer Line 3</td><td>Offer Type</td><td>Offer POS Code</td><td>Offer Amount</td><td>Offer Value</td><td>Edit</td><td>Delete</td></tr>\n";
	foreach( $offers as $offer ) {
		$offer_type = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "offertypes` WHERE `offer_type_id` = '" . $offer->offer_type . "'");
		$page_content .= "<tr><td>" . $offer->offer_id . "</td><td>" . $offer->offer_line1 . "</td><td>" . $offer->offer_line2 . "</td><td>" . $offer->offer_line3 . "</td><td>" . $offer_type->offer_type_name . "</td><td>#" . $offer->offer_pos_code . "</td><td>";

		if($offer->offer_type == 2) {
			$page_content .= round($offer->offer_price, 0) . "%";
		} else {
			$page_content .= "$" . $offer->offer_price;
		}

		$page_content .= "</td><td>$" . $offer->offer_value . "</td><td><a href=\"/admin/offer_edit.php?cid=" . $campaign->campaign_id  . "&amp;oid=" . $offer->offer_id . "\"><img src=\"/admin/assets/edit.png\" alt=\"Edit\" /></a></td><td><a href=\"/admin/handler.offer_delete.php?cid=" . $campaign->campaign_id  . "&amp;oid=" . $offer->offer_id . "\"><img src=\"/admin/assets/delete.png\" alt=\"Delete\" /></a></td></tr>\n";
	}
	$page_content .= "</table>\n";
}

$page_content .= "<p class=\"centered\"><a href=\"/admin/offer_add.php\">Add Offer</a></p>\n";
$page_content .= "<hr />\n";
$page_content .= "<p><a href=\"/admin/index.php\">Back</a> to " . SITE_NAME . " Administration</p>\n";

// Require template
require TEMPLATE_DIR . "/index.php";
?>